package cn.fl.ref.fncprjfstpyinfo.service.impl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjfstpyinfo.service.IFncPrjFstpyInfoService;
import cn.fl.ref.fncprjfstpyinfo.transform.FncPrjDetailVOTransform;
import cn.fl.ref.fncprjfstpyinfo.transform.FncPrjFstpyInfoVOTransform;
import cn.fl.ref.fncprjfstpyinfo.vo.FncPrjDetailVO;
import cn.fl.ref.fncprjfstpyinfo.vo.FncPrjFstpyInfoVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.fncprjfstpyinfo.condition.FncPrjFstpyInfoQueryCondition;
import fl.ref.fncprjfstpyinfo.dto.FncPrjFstpyInfoDTO;
import fl.ref.fncprjfstpyinfo.service.IFncPrjFstpyInfoApiService;

/**
 * @param <E>
 * @Description: 融资项目首付款信息
 */
@Service
public class FncPrjFstpyInfoServiceImpl<E> implements IFncPrjFstpyInfoService {

	private static final Logger logger = LoggerFactory.getLogger(IFncPrjFstpyInfoService.class);

	@Autowired
	private IFncPrjFstpyInfoApiService fncPrjFstpyInfoApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public FncPrjFstpyInfoVO selectById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		FncPrjFstpyInfoDTO dto = this.fncPrjFstpyInfoApiService.selectById(id);
		return FncPrjFstpyInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncPrjFstpyInfoVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		// 系统日志-有用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPrjFstpyInfo数据.", CurrentThreadContext.getCurrentUserName());

		// 系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPrjFstpyInfo数据.");
		return this.fncPrjFstpyInfoApiService.add(FncPrjFstpyInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fncPrjFstpyInfoApiService.removeById(id);
	}

	@Override
	public boolean modify(FncPrjDetailVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}

		return this.fncPrjFstpyInfoApiService.modifyFncPrjDetail(FncPrjDetailVOTransform.toDTO(entity));
	}

	@Override
	public boolean modifyFncPrjDetail(FncPrjDetailVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fncPrjFstpyInfoApiService.modifyFncPrjDetail(FncPrjDetailVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return this.fncPrjFstpyInfoApiService.searchListPage(page);
	}

	@Override
	public List<FncPrjFstpyInfoVO> searchList(FncPrjFstpyInfoQueryCondition condition) {
		return FncPrjFstpyInfoVOTransform.toVOList(this.fncPrjFstpyInfoApiService.searchList(condition));
	}

	@Override
	public FncPrjDetailVO selectByFncPrjId(Long fncPrjId) throws BaseException {
		return FncPrjDetailVOTransform.toVO(this.fncPrjFstpyInfoApiService.selectByFncPrjId(fncPrjId));
	}

	@Override
	public Pager selectFncPrjDetailListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return this.fncPrjFstpyInfoApiService.selectFncPrjDetailListPage(page);
	}

	@Override
	public List<KeyValueVO> getKvList(String grpCd) throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd(), grpCd);
			kvList = transformToKeyValue(sysDictionaryDTOList, SysDictionaryDTO.class.getName(), "code", "codeNm");
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组" + grpCd + "失败.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组" + grpCd + "失败,系统错误.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}

	@Override
	public <E> List<KeyValueVO> transformToKeyValue(List<E> list, String className, String keyName, String valueName) throws BaseException {
		Object ot;
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			ot = Class.forName(className).newInstance();
			Class<? extends Object> clazz = ot.getClass();

			PropertyDescriptor keyNameP = new PropertyDescriptor(keyName, clazz);// 获取 clazz 类型中的 propertyName 的属性描述器
			PropertyDescriptor valueNameP = new PropertyDescriptor(valueName, clazz); // 获取 clazz 类型中的 propertyName 的属性描述器
			Method getKey = keyNameP.getReadMethod();// 从属性描述器中获取 get 方法
			Method getValue = valueNameP.getReadMethod();// 从属性描述器中获取 get 方法

			if (CollectionUtils.isNotEmpty(list)) {
				for (E entity : list) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(getKey.invoke(entity).toString());
					keyValueVO.setValue(getValue.invoke(entity).toString());
					kvList.add(keyValueVO);
				}
			}
		} catch (Exception e) {
			BaseLogUtils.error(logger, "-", "异常", e);
		}
		return kvList;
	}

	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException {
		if (StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())) {
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.fncPrjFstpyInfoApiService.addFlowStart(flowStartParamsDTO);
	}

	@Override
	public FncPrjFstpyInfoVO selectByScmChrgId(FncPrjFstpyInfoQueryCondition condition) throws BaseException {
		FncPrjFstpyInfoDTO fncPrjFstpyInfoDTO = this.fncPrjFstpyInfoApiService.selectByScmChrgId(condition);
		if ( null != fncPrjFstpyInfoDTO) {
			return FncPrjFstpyInfoVOTransform.toVO(fncPrjFstpyInfoDTO);
		}
		return null;
	}
	
	
	@Override
	 public List<KeyValueVO> searchWthrKVList()throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		
		KeyValueVO keyValueVO1 = new KeyValueVO();
		keyValueVO1.setKey("0");
		keyValueVO1.setValue("否");
		kvList.add(keyValueVO1);
		
		KeyValueVO keyValueVO = new KeyValueVO();
		keyValueVO.setKey("1");
		keyValueVO.setValue("是");
		kvList.add(keyValueVO);
		
		KeyValueVO keyValueVO2 = new KeyValueVO();
		keyValueVO2.setKey("2");
		keyValueVO2.setValue("是/否");
		kvList.add(keyValueVO2);
		return kvList;
	}

	/**
	 * 获取保证金所有之和（计入首付）
	 * @param qc
	 * @return
	 */
	public double getFncPrjFstpyBondAll(FncPrjFstpyInfoQueryCondition qc){

		BigDecimal bondAll = BigDecimal.ZERO;//总共保证金额
		
		List<FncPrjFstpyInfoDTO> fncPrjFstpyInfoDTOList = this.fncPrjFstpyInfoApiService.searchList(qc);
		if(fncPrjFstpyInfoDTOList != null && fncPrjFstpyInfoDTOList.size()>0) {
			for (FncPrjFstpyInfoDTO fncPrjFstpyInfoDTO : fncPrjFstpyInfoDTOList) {
				if(fncPrjFstpyInfoDTO.getMrgnIs() != null && fncPrjFstpyInfoDTO.getMrgnIs()== 1) {//是否保证金(0否 1是)
					if(fncPrjFstpyInfoDTO.getPayAmt() != null) {//保证金额
						bondAll = bondAll.add(fncPrjFstpyInfoDTO.getPayAmt());
						bondAll=bondAll.multiply(new BigDecimal("0.01"));
					}
				}
			}
		}
		
		return bondAll.doubleValue();
	}
}
